package org.jeecg.modules.quartz.job;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.RedisUtil;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 自动清除多余token
 * 
 * @Author cxj
 */
@Service
@Slf4j
public class TokenClearJob {
	@Autowired
	private RedisUtil redisUtil;

	@XxlJob("tokenClearJob")
	public ReturnT<String> tokenClearJob(String params) throws Exception {
		int maxSize = 8;
		Set<String> keys = redisUtil.keys(CommonConstant.PREFIX_TOKEN_USER);
		JSONObject list = new JSONObject();
		List<String> tklist = new ArrayList<>();
		for(String key : keys) {
			String token = key.replace("prefix_token_user_", "");
			String username = (String) redisUtil.get(key);
			if(list.containsKey(username)) {
				JSONArray times = list.getJSONArray(username);
				long exptime = redisUtil.getExpire(key);
				JSONObject item = new JSONObject();
				item.put("token", token);
				item.put("time", exptime);
				if(times.size() < maxSize) {
					times.add(item);
					tklist.add(token);
					for(int i = 0; i < times.size() - 1; i++) {
						for(int j = 0; j < times.size() - i - 1; j++) {
							if(times.getJSONObject(j + 1).getLong("time") < times.getJSONObject(j).getLong("time")) {
								JSONObject temp = times.getJSONObject(j);
								times.set(j, times.getJSONObject(j + 1));
								times.set(j + 1, temp);
							}
						}
					}
				} else {
					if(exptime > times.getJSONObject(0).getLong("time")) {
						// 把最小的挤掉
						tklist.remove(times.getJSONObject(0).getString("token"));
						for(int i = maxSize - 1; i >= 1; i--) {
							times.set(i-1, times.getJSONObject(i));
						}
						times.set(maxSize - 1, item);
						tklist.add(token);
						for(int i = 0; i < times.size() - 1; i++) {
							for(int j = 0; j < times.size() - i - 1; j++) {
								if(times.getJSONObject(j + 1).getLong("time") < times.getJSONObject(j).getLong("time")) {
									JSONObject temp = times.getJSONObject(j);
									times.set(j, times.getJSONObject(j + 1));
									times.set(j + 1, temp);
								}
							}
						}
					}
				}
			} else {
				JSONArray times = new JSONArray();
				JSONObject item = new JSONObject();
				item.put("token", key);
				item.put("time", redisUtil.getExpire(key));
				times.add(item);
				tklist.add(token);
				list.put(username, times);
			}
		}
		int counts = 0;
		for(String key : keys) {
			String token = key.replace("prefix_token_user_", "");
			if(!tklist.contains(token)) {
				counts++;
				redisUtil.del(CommonConstant.PREFIX_USER_TOKEN + token, CommonConstant.PREFIX_TOKEN_USER + token);
			}
		}
		log.info(String.format("Jeecg-Boot 删除多余token  时间:" + DateUtils.getTimestamp() + " 数量:" + counts + "个"));
		XxlJobLogger.log("Jeecg-Boot 删除多余token  时间:" + DateUtils.getTimestamp() + " 数量:" + counts + "个");
		return ReturnT.SUCCESS;
	}
}
